#pragma once

#include "GIFHelper.h"
#include <limits>

using namespace Microsoft::WRL;


struct FrameMetadata{
	UINT frameIndex  = UINT_MAX;
	D2D1_RECT_F rect = D2D1::RectF();
	UINT delay = 0;
	UINT disposal = 0;
	bool transparent = false;
};

class GIFDecoder{
	
	HRESULT ReadMetadata();
	HRESULT GetBackgroundColor(IWICMetadataQueryReader *pMetadataQueryReader);

	// Resources
	ComPtr<IWICBitmapDecoder> m_decoder;
	ComPtr<ID2D1BitmapRenderTarget> m_renderTarget;

	// Metadata
	UINT m_frames_count;
	D2D1::ColorF m_background_color;
	UINT m_width;
	UINT m_height;
	UINT m_pixel_width;
	UINT m_pixel_height;
	UINT m_max_loop_count;
	BOOL m_has_loop;

public:
	GIFDecoder(const wchar_t* fileName);
	GIFDecoder(IStream* stream);

	HRESULT GetFrameAndMetadata(UINT uFrameIndex, UINT width, UINT height, FrameMetadata &data, ID2D1Bitmap1* &frame);
	Windows::Foundation::Size GetSize() const{ return Windows::Foundation::Size(static_cast<float>(m_width), static_cast<float>(m_height)); }
	D2D1::ColorF GetBackgroundColor() const{ return m_background_color; }
	UINT GetFramesCount() const{ return m_frames_count; }
};